<template>
  <div class="login-container">
    <el-alert v-show="false" title="后台管理" type="success" :closable="false" style="position: fixed"></el-alert>
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="14" :xl="14">
        <div style="color: transparent">左侧区域占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10">
        <el-form status-icon class="login-form">
          <div class="form-header">
            <div class="title">hello !</div>
            <div class="title-tips">
              欢迎来到后台管理！
            </div>
          </div>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" autocomplete="off" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="captchaCode">
            <el-row>
              <el-col :span="15">
                <el-input autocomplete="off" v-model="loginForm.captchaCode" placeholder="请输入验证码" />
              </el-col>
              <el-col :span="8" :offset="1">
                <el-image fit="contain" :src="image" @click="parseCaptcha()" style="height: 100%; background: white" class="pointer" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-button class="login-btn" @click="parseLogin()" type="primary" size="large">
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">

import { User, Lock } from '@element-plus/icons-vue'
import { ref , onMounted } from 'vue';
import { getCaptcha ,LoginType } from '@/api/login/index';
import LoginStore from '@/store/modules/login/loginStore';
import { useRouter } from 'vue-router';
import { ElNotification } from 'element-plus';

let loginForm = ref<LoginType>(
  {
    username:"",
    password:"",
    captchaCode:"",
    captchaKey:""
  }
);
let user = LoginStore();
let router = useRouter();

let parseLogin = async ()=>{
  await user.doLogin(loginForm.value);
  ElNotification({
    title: '登录成功',
    message: '欢迎光临!!!',
    type: 'success',
  })
  router.push("/");
}


let image = ref("");

let parseCaptcha = async ()=>{
  let {code,data} = await getCaptcha();
  if(code == 200){
    image.value = data.image;
    loginForm.value.captchaKey = data.key;
  }
}

onMounted(()=>{
  parseCaptcha();
});


</script>

<style scoped lang="scss">
@import './index';
</style>
